package cn.exrick.xboot.modules.activiti.serviceimpl;

import cn.exrick.xboot.base.XbootBaseDao;
import cn.exrick.xboot.common.vo.SearchVo;
import cn.exrick.xboot.modules.activiti.dao.mapper.ProcessInstanceMapper;
import cn.exrick.xboot.modules.activiti.vo.ActHistoricProcessInstance;
import cn.exrick.xboot.modules.activiti.vo.ActProcessInstance;
import cn.exrick.xboot.modules.activiti.service.ProcessInstanceService;
import cn.exrick.xboot.modules.activiti.vo.YongLaiChuanCan;
import cn.exrick.xboot.modules.base.dao.DepartmentDao;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import cn.exrick.xboot.common.utils.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;


@Slf4j
@Service
@Transactional
public class ProcessInstanceServiceImpl implements ProcessInstanceService {

    @Autowired
    private ProcessInstanceMapper processInstanceMapper;

    @Autowired
    private SecurityUtil securityUtil;

    @Autowired
    private DepartmentDao departmentDao;

    @Override
    public XbootBaseDao<ActProcessInstance, String> getRepository() {
        return null;
    }

    @Override
    public List<ActProcessInstance> findByNameDepIdKeyTime(YongLaiChuanCan chuanCan,SearchVo searchVo) {
        //默认查询两个月的数据
        if (searchVo.getStartDate()=="" || searchVo.getStartDate()==null){
            if (searchVo.getStartDate()=="" || searchVo.getStartDate()==null){
                Date da = new Date();//获取当前时间
                Calendar calendar = Calendar.getInstance(); //得到日历
                calendar.setTime(da);//把当前时间赋给日历
                calendar.add(calendar.MONTH, -2); //设置为前2月 MONTH DAY_OF_MONTH
                da = calendar.getTime();//获取2个月前的时间
                //时间格式转换
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
                String dateString = formatter.format(da);
                searchVo.setStartDate(dateString);
            }
        }
        return processInstanceMapper.findByNameDepIdKeyTimeStr(chuanCan,searchVo);
    }

    @Override
    public List<ActHistoricProcessInstance> findHistoricByDepIdKeyTime(YongLaiChuanCan chuanCan, SearchVo searchVo) {
        //默认查询两个月的数据
        if (searchVo.getStartDate()=="" || searchVo.getStartDate()==null){
            if (searchVo.getEndDate() =="" || searchVo.getEndDate() ==null){
                Date da = new Date();//获取当前时间
                Calendar calendar = Calendar.getInstance(); //得到日历
                calendar.setTime(da);//把当前时间赋给日历
                calendar.add(calendar.MONTH, -2); //设置为前2月 MONTH DAY_OF_MONTH
                da = calendar.getTime();//获取2个月前的时间
                //时间格式转换
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
                String dateString = formatter.format(da);
                searchVo.setStartDate(dateString);
            }
        }
        List<ActHistoricProcessInstance> historicByDepIdKeyTime = new LinkedList<>();
        List<ActHistoricProcessInstance> arr = processInstanceMapper.findHistoricByDepIdKeyTimeStr(chuanCan,searchVo);
        String departmentId = securityUtil.getCurrUser().getDepartmentId();
        String parentId = departmentDao.getOne(departmentId).getParentId();

        if ("0".equals(parentId)){
            historicByDepIdKeyTime = arr;
        }else {
            for (int i=0;i<arr.size();i++){
                try {
                    if(arr.get(i).getParentId().equals(departmentId)){
                        historicByDepIdKeyTime.add(arr.get(i));
                    }
                    if (departmentId.equals(arr.get(i).getDepartmentId())) {
                        historicByDepIdKeyTime.add(arr.get(i));
                    }
                }catch (Exception e){
                    historicByDepIdKeyTime.add(arr.get(i));
                }

            }
        }
        return historicByDepIdKeyTime;
    }
}
